/* global window */
import modelExtend from 'dva-model-extend'
import pathToRegexp from 'path-to-regexp'
import * as basicData from 'services/basicData.js'
import { list, create, update, remove } from 'services/customerSurplus'
import { message } from 'antd'
import { pageModel } from './common'


export default modelExtend(pageModel, {
  namespace: 'customerSurplusData',

  state: {
    currenciesData: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((pathname) => {
        const match = pathToRegexp('/customerSurplus').exec(pathname.pathname)
        if (match) {
          dispatch({ type: 'list' })
          dispatch({ type: 'iniBasicData' })
        }
      })
    },
  },

  effects: {
    * list ({ payload = {} }, { call, put }) {
      const data = yield call(list, payload)
      const listData = data.data
      if (data.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },

    * showModal ({ payload }, { call, put }) {
      yield put({
        type: 'updateModalState',
        payload: { ...payload },
      })
    },

    * iniBasicData({ payload = {} }, { call, put }) {
      const currenciesRes = yield call(basicData.listall, { type: 'Currencies' })
      if (currenciesRes.success) {
        yield put({
          type: 'updateState',
          payload: {
            currenciesData: currenciesRes.data,
          },
        })
      }
    },

  },

  reducers: {

  },
})
